package com.dqhl.community.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.adapter.ShoppingDetailAdapter;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.util.ScreenUtils;
import com.dqhl.community.view.SectionSelectPopupWindow;

/**
 * Created by LIENLIN on 2016/9/2.
 * 周边购物-商家特色详情页
 */
public class ShoppingDetailActivity extends BaseActivity implements View.OnClickListener {

    private ImageView mIvback;
    private TextView mTvTitle;
    private TextView mTvAllMoney;//总金额
    private EditText mEtShangHu;//收索商户
    private ImageView mImOperation;

    private RelativeLayout mRelative;
    private ImageView mIvRight;
    private SectionSelectPopupWindow sectionSelectPopupWindow;
    private ListView mLvShopping;
    private TextView mTvAccount;//结账按钮
    private int money=0;//总金额
    private int number=0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_detail);

        initView();
    }

    private void initView() {
        mIvback = (ImageView) findViewById(R.id.iv_top_back);
        mTvTitle = (TextView) findViewById(R.id.tv_top_title);
        mTvAllMoney = (TextView) findViewById(R.id.tv_shopping_allmoney);
        mImOperation = (ImageView) findViewById(R.id.tv_top_operation);
        mEtShangHu = (EditText) findViewById(R.id.et_shopdetail);
        mRelative = (RelativeLayout) findViewById(R.id.relative_realtime);
        mIvRight = (ImageView) findViewById(R.id.iv_right_realtime);
        mLvShopping = (ListView) findViewById(R.id.lv_shopping);
        mTvAccount = (TextView) findViewById(R.id.tv_account);

        mIvback.setVisibility(View.VISIBLE);
        mImOperation.setVisibility(View.INVISIBLE);
        mIvback.setOnClickListener(this);
        mTvTitle.setText(R.string.txt_shoping);
        mRelative.setOnClickListener(this);
        mTvAccount.setOnClickListener(this);
        //动态设置lv高度
        int height= ScreenUtils.getScreenHeight(this);
        int px=ScreenUtils.dp2px(this, 160);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mLvShopping.getLayoutParams();
        params.height=height-px;
        mLvShopping.setLayoutParams(params);

        mLvShopping.setDividerHeight(0);
        final ShoppingDetailAdapter adapter = new ShoppingDetailAdapter(this);
        mLvShopping.setAdapter(adapter);
        mLvShopping.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //跳转购物详情页
                ShoppingDetailAdapter.ViewHolder vh = (ShoppingDetailAdapter.ViewHolder) view.getTag();
//                toast(vh.mTvNumber.getText().toString()+"");
                Bundle bundle=new Bundle();
//                bundle.putInt("number",Integer.valueOf(vh.mTvNumber.getText().toString()));
                bundle.putInt("number",Integer.valueOf(vh.num));
                overlay(ShoppingDetailItemActivity.class,bundle);
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.tv_account:
                //跳转结账页
                if (money>0){
                    Bundle bundle=new Bundle();
//                    bundle.putString("allmoney",mTvAllMoney.getText().toString());
//                    overlay(ShoppingAccount.class,bundle);
                    //TODO 待服务器返回真是数据
                    bundle.putString("total_money", "18");
                    bundle.putInt("number", 1);
                    bundle.putString("img", "");
                    bundle.putString("name", "炒西红柿");
                    bundle.putString("goods_id", "0");
                    bundle.putString("price", "18");
                    overlay(ShoppingPaymentActivity.class, bundle);
                }else {
                    toast("请选择商品");
                }

                break;
        }
    }
    /**
     * 总金额的回调函数
     * @param m
     */
    public void getMoney(int m){
        money+=m;
        mTvAllMoney.setText("¥"+money);
    }
    /**
     * 总金额的回调函数
     * @param m
     */
    public void getNumber(int m){
        number=m;
    }

}
